package com.nearby.demand.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.auto.layout.lib.AutoLayoutActivity;
import com.nearby.demand.R;
import com.nearby.demand.adapter.DividerItemDecoration;
import com.nearby.demand.adapter.WorldDetailAdapter;
import com.nearby.demand.adapter.listener.OnItemClickListener;
import com.nearby.demand.db.Demand;
import com.nearby.demand.model.News;
import com.nearby.demand.ui.view.WorldDetailView;
import com.nearby.demand.utils.KLog;
import com.nearby.demand.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Administrator on 2016/home_1/19.
 */
public class UiWorldDetail extends AutoLayoutActivity implements WorldDetailView {


    @Bind(R.id.ptr_classic_frame_world_detail)
    PtrClassicFrameLayout ptrClassicFrameWorldDetail;
    @Bind(R.id.recycler_view_world_detail)
    RecyclerView recyclerViewWorldDetail;
    @Bind(R.id.tv_world_detail_chat)
    TextView tvWorldDetailChat;
    @Bind(R.id.tv_world_detail_grab_one)
    TextView tvWorldDetailGrabOne;


    List<News> newses = new ArrayList<>();
    WorldDetailAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_world_detail, 1);
        ButterKnife.bind(this);
        initView();
        initIntent();
    }

    @Override
    public void onCreateCustomToolBar(Toolbar toolbar) {
        super.onCreateCustomToolBar(toolbar);
        //        toolbar.showOverflowMenu();
        toolbar.hideOverflowMenu();
        View view = getLayoutInflater().inflate(R.layout.toolbar_button_is_toolbar, toolbar);
        TextView textView = (TextView) view.findViewById(R.id.toolbar_title);
        textView.setText("订单详情");


    }

    public void initNews() {
        String [] value1 = {"我们一起科技园探讨技术吧","我们一起车公庙喝咖啡吧","我们一起去宝体体育馆打网球吧","我们一起世界之窗开阔视野吧"};
        String [] value2 = {"曹操","刘备","诸葛亮","秦始皇"};
        int  k = 0;
        int j = 0;
        for (int i = 0; i < 5; i++) {
            News news = new News();
            if (i == 0) {
                news.setType(0);
                news.setTitle("00000000");
            } else {
                k = i;
                j = i;
                news.setUrl(value2[--k]);
                news.setType(1);
                news.setTitle(value1[--j]);
            }
            newses.add(news);
        }

    }

    public void initView() {
        initNews();
        ptrClassicFrameWorldDetail.setLastUpdateTimeRelateObject(this);
        ptrClassicFrameWorldDetail.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                ptrClassicFrameWorldDetail.refreshComplete();
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                ptrClassicFrameWorldDetail.refreshComplete();
            }

        });
        // the following are default settings
        ptrClassicFrameWorldDetail.setResistance(1.7f);
        ptrClassicFrameWorldDetail.setRatioOfHeaderHeightToRefresh(1.2f);
        ptrClassicFrameWorldDetail.setDurationToClose(200);
        ptrClassicFrameWorldDetail.setDurationToCloseHeader(1000);
        // default is false
        ptrClassicFrameWorldDetail.setPullToRefresh(false);
        // default is true
        ptrClassicFrameWorldDetail.setKeepHeaderWhenRefresh(true);


        adapter = new WorldDetailAdapter(newses);
        adapter.setActivity(this);
        //            mAdapter.setOnItemClickListener(new OnItemClickListener() {
        //                @Override
        //                public void onClick(int position, View view, Object item) {
        //                    Demand items = (Demand) item;
        //                    Toast.makeText(getActivity(), "短" + items.getStPid(), Toast.LENGTH_SHORT).show();
        //
        //                }
        //            });
        adapter.setOnItemClickListener(new OnItemClickListener<Demand>() {

            @Override
            public void onClick(View view, Demand item) {
                Toast.makeText(UiWorldDetail.this, "短" + item.getStPid(), Toast.LENGTH_SHORT).show();
                startActivity(new Intent(UiWorldDetail.this, UiChat.class).putExtra("toChatUsername", item.getStPid()));
            }
        });
        //
        //            mAdapter.setOnItemLongClickListener(new OnItemLongClickListener<Demand>() {
        //                @Override
        //                public void onLongClick(View view, Demand item) {
        //                    Toast.makeText(getActivity(), "长按", Toast.LENGTH_SHORT).show();
        //                }
        //            });
        recyclerViewWorldDetail.setHasFixedSize(true);//固定尺寸
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(UiWorldDetail.this);
        recyclerViewWorldDetail.setLayoutManager(linearLayoutManager);
        recyclerViewWorldDetail.addItemDecoration(new DividerItemDecoration(UiWorldDetail.this,
                DividerItemDecoration.VERTICAL_LIST));
        recyclerViewWorldDetail.setAdapter(adapter);
    }

    @OnClick(R.id.tv_world_detail_chat)
    public void chat() {
        boolean is_login = (Boolean) SPUtil.get(UiWorldDetail.this, "is_login", false);
        if (is_login) {
            startActivity(new Intent(UiWorldDetail.this, UiChat.class).putExtra("toChatUsername", toChatUsername));
        }else{
            Toast.makeText(this,"你尚未登陆,请登陆",Toast.LENGTH_SHORT).show();
            startActivity(new Intent(UiWorldDetail.this, UiLogin.class));
        }

    }

    private String toChatUsername;

    public void initIntent() {
        toChatUsername = getIntent().getStringExtra("toChatUsername");
        KLog.i("toChatUsername=" + toChatUsername);
    }
}
